<!--
 * @Description: 单位管理页面
-->
<template>
  <BaseMainSlot>
    <template v-slot:btn-box>
      <ActionBar :page-no="8" :add-active="addActive" :del-active="delActive" :has-edit="false" :has-save="false" :has-cancel="false" @add="add" @del="del" />
    </template>
    <template v-slot:search-box>
      <SearchBox @search="search">
        <div class="common-search-item">
          <div class="label">生长品种名称：</div>
          <div class="value">
            <div v-for="(item,index) in plantVarityList" :key="index" :class="{active: plantVarityNo == item.plantVarityNo}" @click="choosePlantVarity(item)">{{ item.plantVarityName }}</div>
          </div>
        </div>
        <div class="common-search-item">
          <div class="label">生长阶段名称：</div>
          <div class="value">
            <div v-for="(item,index) in growthStageList" :key="index" :class="{active: growthStageNo == item.growthStageNo}" @click="chooseGrowthStage(item)">{{ item.growthStageName }}</div>
          </div>
        </div>
      </SearchBox>
    </template>
    <template v-slot:table-box>
      <el-table
        v-loading="loading"
        :data="tableData"
        stripe
        border
        height="calc(100% - 50px)"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column prop="recordName" label="阶段名称" />
        <el-table-column prop="warehouse" label="生长持续天数" />
        <el-table-column prop="product" label="阶段图片">
          <template v-if="scope.row.growthStageNo" slot-scope="scope">
            <el-button class="text-btn-cursor" type="text" size="small">查看图片</el-button>
          </template>
        </el-table-column>
        <el-table-column width="120" label="操作">
          <template v-if="scope.row.growthStageNo" slot-scope="scope">
            <el-button v-if="judgePermission(8, 'edit')" class="text-btn-cursor" type="text" size="small" @click="handleEdit(scope.row)"><i class="el-icon-edit-outline"></i> 修改</el-button>
            <el-button v-if="judgePermission(8, 'delete')" class="text-btn-cursor cursor-delete" type="text" size="small" @click="handleDelete(scope.row)"><i class="el-icon-delete"></i> 删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog
        :title="dialogTitle + '信息'"
        :visible.sync="dialogFormVisible"
        :before-close="cancel"
        width="40%"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          class="el-form"
        >
          <!-- <el-form-item label="记录名称:" prop="recordName" class="w100p">
            <el-input v-model="formData.recordName" clearable />
          </el-form-item>
          <el-form-item label="盘点仓库:" prop="warehouse" class="w100p">
            <el-input v-model="formData.warehouse" clearable />
          </el-form-item>
          <el-form-item label="盘点产品:" prop="product" class="w100p">
            <el-input v-model="formData.product" clearable />
          </el-form-item>
          <el-form-item label="盘点人:" prop="model" class="w100p">
            <el-input v-model="formData.checkedPerson" clearable />
          </el-form-item>
          <el-form-item label="盘点日期:" prop="checkedDate" class="w100p">
            <el-date-picker v-model="formData.checkedDate" align="right" type="date" placeholder="选择日期" />
          </el-form-item> -->
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">
            取 消
          </el-button>
          <el-button class="zh-button-two" type="primary" @click="save('formData')">
            保 存
          </el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import nowpagereq from "@/api/basic-info-manage/unit-manage";
import unitApi from "@/api/basic-info-manage/unit-manage";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot"
import { mymixin } from "@/utils/mixin"
import SearchBox from '@/components/SearchBox';
import { mapGetters } from 'vuex';
export default {
  name: "GrowthStageManage",
  components: { ActionBar, Pagination, BaseMainSlot, SearchBox },
  mixins: [mymixin],
  data() {
    return {
      searchObj: {},
      unitList: [],
      dialogTitle: "",
      dialogFormVisible: false,
      plantVarityList: [],
      growthStageList: []
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created() {
    this.getunitList();
    // 获取表格数据
    this.getTableDataList();
  },
  mounted() { },
  methods: {
    getunitList() {
      let params = {};
      unitApi.getallBasicunitlist(params).then(res => {
        if (res.code === 0) {
          this.$message.error('查询失败')
        }
        let data = res.data;
        this.unitList = data;
      });
    },
    getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          unitName: this.unitName
        }
      };
      nowpagereq.getPlantingUnitList(params).then(res => {
        this.loading = false;
        if (res.code === 0) {
          this.$message.error('查询失败')
        }
        let data = res.data;
        // 赋值总条数
        this.totalSize = data.totalElements;
        // 赋值数组
        this.tableData = data.content;
        this.addblankmet()
        this.resetActionBar();
      });
      // }
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    async handleEdit(row) {
      if (row.unitId) {
        this.openDialog('edit')
        this.formData = JSON.parse(JSON.stringify(row));
      }
    },
    // 添加
    add() {
      this.openDialog('add')
      this.formData = {}
    },
    // 取消
    cancel() {
      this.closeDialog();
      this.formData = {}
    },
    // 初始化操作栏状态
    resetActionBar() {
      this.nowChanceStau = ''
      this.formData = {}
    },
    handleSelectionChange(list) {
      this.selectedPlantingUnitList = [];
      // let notvalue = true
      this.selectedPlantingUnitList = list.map(item => {
        if (item.unitNo) {
          return item.unitNo
        } else {
          // notvalue = false
          return ""
        }
      });
    },
    delmessage() {
      nowpagereq.delPlantingUnitById(this.selectedPlantingUnitList).then(
        res => {
          if (res.code === 1) {
            this.$message.success('删除成功')
            this.getTableDataList();
          } else {
            this.$message.error('删除失败')
          }
        }
      )
    },
    handleDelete(data) {
      nowpagereq.delPlantingUnitById([data.unitNo]).then(res => {
        if (res.code === 1) {
          this.$message.success('删除成功')
          this.getTableDataList();
        } else {
          this.$message.error('删除失败')
        }
      })
    },
    save() {
      if (this.nowChanceStau === 'edit') {
        // 编辑接口
        nowpagereq.editPlantingUnitInfo(this.formData).then(res => {
          if (res.code === 1) {
            this.$message.success("修改成功！");
            this.closeDialog();
            this.getTableDataList();
          } else {
            this.$message.error("修改失败！");
          }
        });
      } else if (this.nowChanceStau === 'add') {
        // 新增接口
        nowpagereq.addPlantingUnitInfo(this.formData).then(res => {
          if (res.code === 1) {
            this.$message.success("新增成功！");
            this.closeDialog();
            this.getTableDataList();
          } else {
            this.$message.error("新增失败！");
          }
        });
      }
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      this.dialogTitle = type === 'add' ? '新增' : '编辑'
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = '';
    }
  }
};
</script>

<style scoped lang="scss">
.df {
  display: flex;
  align-items: center;
  width: 100%;
}

.dfs {
  ::v-deep .el-form-item__content {
    flex-direction: column;
  }
  i {
    font-size: 30px;
    margin-left: 10px;
    cursor: pointer;
  }
}

.tags {
  width: 100%;
  display: flex;
  flex-wrap: wrap;

  .tag-item {
    height: 30px;
    padding: 5px 10px;
    background-color: #00a1c2;
    border-radius: 8px;
    display: flex;
    align-items: center;
    margin-top: 10px;
    margin-right: 10px;

    i {
      font-size: 14px;
    }
  }
}

</style>
